//
//  SDianyingBaseManager.m
//  News
//
//  Created by dlios on 15-5-23.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SDianyingBaseManager.h"

@implementation SDianyingBaseManager

+ (SDianyingBaseManager *)shareInstance
{
    static SDianyingBaseManager *dbManager = nil;
    if (dbManager == nil) {
        dbManager = [[SDianyingBaseManager alloc] init];
        
    }
    return dbManager;
}
//打开数据库
- (void)openDB
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *dbPath = [path stringByAppendingPathComponent:@"dianying.db"];
    int result = sqlite3_open([dbPath UTF8String], &dbPoint);
    NSLog(@"打开结果 :%d, path : %@", result, dbPath);
}
- (void)createTable
{
    NSString *createSQL = @"create table if not exists dianying(pingfen integer, large text, Mytitle text, renshu text, guojia text, leixing text, xiangqing text, shichang text, num integer primary key autoincrement)";
    int result = sqlite3_exec(dbPoint, createSQL.UTF8String, NULL, NULL, nil);
    if (SQLITE_OK == result) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建表失败");
    }
}
- (void)insertInfoWithSshoucang:(Sshoucang *)shoucang
{
    NSString *insertStr = [NSString stringWithFormat:@"insert into dianying(pingfen, large, Mytitle, renshu, guojia, leixing, xiangqing, shichang) values ('%ld', '%@', '%@', '%@', '%@', '%@', '%@', '%@')", shoucang.pingfen, shoucang.large, shoucang.Mytitle, shoucang.renshu, shoucang.guojia, shoucang.leixing, shoucang.xiamgqing, shoucang.shichang];
    int result = sqlite3_exec(dbPoint, insertStr.UTF8String, NULL, NULL, nil);
    if (SQLITE_OK == result) {
        NSLog(@"插入成功");
    }else {
        NSLog(@"插入失败");
    }
}

- (NSMutableArray *)selectInfo
{
    NSString *selectSQL = @"select * from dianying";
    sqlite3_stmt *stmt = nil;
    int result = sqlite3_prepare_v2(dbPoint, selectSQL.UTF8String, -1, &stmt, nil);
    NSMutableArray *arr = [NSMutableArray array];
    if (SQLITE_OK == result) {
        NSLog(@"查询准备成功");
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
             int pingfen = sqlite3_column_int(stmt, 0);
            const unsigned char *large = sqlite3_column_text(stmt, 1);
            const unsigned char *Mytitle = sqlite3_column_text(stmt, 2);
            const unsigned char *renshu = sqlite3_column_text(stmt, 3);
            const unsigned char *guojia = sqlite3_column_text(stmt, 4);
            const unsigned char *leixing = sqlite3_column_text(stmt, 5);
            const unsigned char *xiangqing = sqlite3_column_text(stmt, 6);
            const unsigned char *shichang = sqlite3_column_text(stmt, 7);
            int num = sqlite3_column_int(stmt, 8);
            
            Sshoucang *shoucang = [[[Sshoucang alloc] init] autorelease];
            
            shoucang.pingfen = pingfen;
            shoucang.large = [NSString stringWithUTF8String:(const char *)large];
            shoucang.Mytitle = [NSString stringWithUTF8String:(const char *)Mytitle];
            shoucang.renshu = [NSString stringWithUTF8String:(const char *) renshu];
            shoucang.guojia = [NSString stringWithUTF8String:(const char *) guojia];
            shoucang.leixing = [NSString stringWithUTF8String:(const char *) leixing];
            shoucang.xiamgqing = [NSString stringWithUTF8String:(const char *)xiangqing];
            shoucang.shichang = [NSString stringWithUTF8String:(const char *)shichang];
            shoucang.num = num;
            
            [arr addObject:shoucang];
        }
    }else{
        NSLog(@"查询准备失败");
    }
    return  arr;
    
}
- (void)deleteInfoWithTitle:(NSString *)title
{
    //1.创建sql语句
    NSString *deleteSQL = [NSString stringWithFormat:@"delete from dianying where Mytitle = '%@'", title];
    //2.执行语句
    int result = sqlite3_exec(dbPoint, deleteSQL.UTF8String, NULL, NULL, nil);
    //3.判断
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }

}
//删除表
- (void)dropTable
{
    //1.sql语句
    NSString *dropSQL = @"drop table dianying";
    //2.执行
    int result = sqlite3_exec(dbPoint, dropSQL.UTF8String, NULL, NULL, nil);
    //3.判断
    if (result == SQLITE_OK) {
        NSLog(@"删除表成功");
    }else{
        NSLog(@"删除表失败");
    }
}


@end
